layui.define(["layer", "jquery"], function (exports) {
  var $ = layui.jquery;

  var common = {
    //接口地址
    // api: "http://localhost:8080", //本地
    api:'http://111.229.125.244:8082',//阿里云

    ajax: function (url, data, ft, method, errorft, async, showLoading) {
      // 按需展示loading
      var loadIdx;
      if (showLoading) {
        loadIdx = top.layer.load(2);
      }

      $.ajax({
        url: url,
        data: data,
        type: method == undefined ? "get" : method,
        async: async == undefined ? true : async,
        contentType: "application/json; charset=UTF-8",
        timeout: 10000,
        dataType: "json",
        beforeSend: function (request) {
          request.setRequestHeader("authorization", common.getToken("token"));
        },
        success: function (res) {
          if (loadIdx) {
            top.layer.close(loadIdx);
          }

          if (res.code != 200 && res.code != undefined) {
            if (res.code == 5000) {
              layer.msg("请登录", { icon: 2, time: 3000 }, function () {
                setTimeout(() => {
                  window.location.href = "/login.html";
                }, 3000);
              });
            } else if (res.code == 5001) {
              layer.msg("用户没有操作权限", { icon: 2, time: 2000 });
            } else if (res.code == 999) {
              // 账号在其它地方登录时当前登录关闭定时器
              window.clearInterval(data);
              // 账号在别处登录，需要提示用户并返回登录页面
              layer.alert(res.msg, { icon: 2, closeBtn: 0 }, function () {
                window.location.href = "/login.html";
              });
            }

            if (errorft != null && errorft != undefined) {
              errorft(res);
            }
          } else {
            if (ft != null && ft != undefined) {
              ft(res);
            }
          }
        },
        error: function () {
          if (loadIdx) {
            top.layer.close(loadIdx);
          }
          layer.msg("服务器好像出了点问题！请稍后试试");
        },
      });
    },

    login: function (res) {
      if (res.code == 5000) {
        layer.msg("请登录", { icon: 2, time: 1000 }, function () {
          window.location.href = "/login.html";
        });
      } else if (res.code == 5001) {
        layer.msg("用户没有权限操作", { icon: 2, time: 1000 });
      } else {
        layer.msg("请登录", { icon: 2, time: 1000 }, function () {
          window.location.href = "/login.html";
        });
      }
    },
    //设置token
    setToken: function (value) {
      return layui.data("token", {
        key: "token",
        value: value,
      });
    },
    //获取token
    getToken: function () {
      return layui.data("token").token;
    },
    compare: function (data) {
      var compare = function (obj1, obj2) {
        var val1 = obj1.id;
        var val2 = obj2.id;
        if (val1 < val2) {
          return -1;
        } else if (val1 > val2) {
          return 1;
        } else {
          return 0;
        }
      };
      return data.sort(compare);
    },
    //获取当前时间
    getCurrentTime: function () {
      var date = new Date();
      var time =
        date.getFullYear() +
        "年" +
        (date.getMonth() + 1) +
        "月" +
        date.getDay() +
        "日 " +
        date.getHours() +
        "时" +
        date.getMinutes() +
        "分" +
        date.getSeconds() +
        "秒";
      return time;
    },
  };

  exports("common", common);
});
